package com.ljph.kit;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.ljph.jfinal.dto.ApiResult;

import java.util.List;

/**
 * Created by yuzhou on 16/8/11.
 */
public class PageKit {

    private static int defautPageSize = 10;
    private static String pageKey = "page";
    private static String pageSizeKey = "rows";
    private static String totalPageKey = "totalPage";

    public static void setPageKey(String pageKey) {
        PageKit.pageKey = pageKey;
    }

    public static void setPageSizeKey(String pageSizeKey) {
        PageKit.pageSizeKey = pageSizeKey;
    }

    public static void setTotalPageKey(String totalPageKey) {
        PageKit.totalPageKey = totalPageKey;
    }

    /**
     * 获取页码
     * @param controller
     * @return
     */
    public static int getPageNumber (Controller controller) {
        // 页码
        int pn = 1;
        if (RegExpKit.checkPositiveInteger(controller.getPara(pageKey))) {
            pn = Integer.parseInt(controller.getPara(pageKey));

            if(pn < 1){
                pn=1;
            }

            if(RegExpKit.checkPositiveInteger(controller.getPara(totalPageKey))){
                int pt=Integer.parseInt(controller.getPara(totalPageKey));
                if(pn > pt){
                    pn = pt;
                }
            }
        }
        return pn;
    }

    /**
     * 获取每页包含记录条数
     * @param controller
     * @return
     */
    public static int getPageSize (Controller controller) {
        int ps = defautPageSize;
        if (RegExpKit.checkPositiveInteger(controller.getPara(pageSizeKey))) {
            ps = Integer.parseInt(controller.getPara(pageSizeKey));
            if(ps < 1){
                ps = defautPageSize;
            }
        }
        return ps;
    }
}
